package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

@RestController
public class HelloController {

    @Autowired
    @Qualifier("messageSource") //@Autowired也可以按照组件id注入 👉 按照成员变量作为组件id注入
    MessageSource messageSource;

    @RequestMapping("hello/{key}")
    public BaseRespVo hello(Locale locale, @PathVariable("key") String key,String[] hobbys) {
        /**
         * 第一个参数：配置文件中的key
         * 第二个参数：占位符对应的值
         * 第三个参数：locale信息
         */
        //String message = messageSource.getMessage(key, null, locale);
        String message = messageSource.getMessage(key, hobbys, locale);
        return BaseRespVo.ok(null,message);
    }

    @RequestMapping("valid/param")
    public BaseRespVo valid(HttpServletRequest request) {
        String username = request.getParameter("username");
        //手写业务
        if (username == null || username.length() < 7) {
            return BaseRespVo.fail("用户名过短");
        }
        String password = request.getParameter("password");
        if (password == null || password.length() < 7) {
            return BaseRespVo.fail("密码太短");
        }
        return BaseRespVo.ok();

    }
}
